package com.bawei.module_publish.openlive.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_publish.R
import com.bawei.module_publish.openlive.model.entity.GoodRecord
import com.bumptech.glide.Glide

class GoodsAdapter(
    private val list: MutableList<GoodRecord>,
    private val onSelectionChanged: () -> Unit
) : RecyclerView.Adapter<GoodsAdapter.GoodsViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodsViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_mygoods_goods, parent, false)
        return GoodsViewHolder(view)
    }

    override fun getItemCount(): Int = list.size

    fun setData(newList: List<GoodRecord?>) {
        list.clear()
        list.addAll(newList.filterNotNull())
        notifyDataSetChanged()
    }
    // 存储选中项的位置
    val selectedSet = mutableSetOf<Int>()

    // 获取选中商品列表
    fun getSelectedItems(): List<GoodRecord> = list.filterIndexed { index, _ -> index in selectedSet }

    // 获取选中数量
    fun getSelectedCount(): Int = selectedSet.size

    // 切换选中状态
    fun toggleSelection(position: Int) {
        if (selectedSet.contains(position)) {
            selectedSet.remove(position)
        } else {
            selectedSet.add(position)
        }
        notifyItemChanged(position)
        onSelectionChanged() // 通知状态变化
    }
    override fun onBindViewHolder(holder: GoodsViewHolder, position: Int) {
        val good = list[position]
        holder.bind(good)
        holder.checkBox.setOnClickListener(null) // 防止复用导致多次回调
        // 设置 CheckBox 选中状态
        holder.checkBox.isChecked = position in selectedSet

        // 处理 CheckBox 点击事件
        holder.checkBox.setOnClickListener {
            toggleSelection(position)
        }
    }

    class GoodsViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val checkBox: CheckBox = view.findViewById(R.id.checkbox_open)
        private val ivImg: ImageView = view.findViewById(R.id.iv_goods_img)
        private val tvTitle: TextView = view.findViewById(R.id.tv_title)
        private val layoutTags: LinearLayout = view.findViewById(R.id.layout_tags)
        private val tvPrice: TextView = view.findViewById(R.id.tv_price)
        private val tvCommission: TextView = view.findViewById(R.id.tv_commission)
        private val tvStock: TextView = view.findViewById(R.id.tv_stock)
        fun bind(good: GoodRecord) {
            Glide.with(ivImg.context).load(good.mainImgUrl).placeholder(R.drawable.bg_goods_img).into(ivImg)
            tvTitle.text = good.title
            layoutTags.removeAllViews()
            tvPrice.text = "￥${good.marketPriceFee}"
            tvCommission.text = "5%赚￥3.5"
            tvStock.text = "10万+"
        }
    }
}
